{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 文本数据转换处理\n",
    "import numpy as np # 导入numpy库\n",
    "from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer # 导入文本特征提取函数\n",
    "from sklearn.svm import LinearSVC # 导入线性支持向量机分类模型\n",
    "from sklearn.datasets import fetch_20newsgroups # 导入20类新闻数据集\n",
    "categories = ['misc.forsale', 'rec.autos','comp.graphics', 'sci.med'] # 选择4个类别\n",
    "remove = ('headers', 'footers', 'quotes') # 移除头部、尾部和引用内容\n",
    "twenty_train = fetch_20newsgroups(subset='train',\n",
    "                                  remove=remove,\n",
    "                                  categories=categories)  # 训练数据\n",
    "twenty_test = fetch_20newsgroups(subset='test',\n",
    "                                 remove=remove,\n",
    "                                 categories=categories)  # 验证数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\admin\\anaconda3\\envs\\ml\\lib\\site-packages\\sklearn\\svm\\_base.py:1208: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n",
      "  ConvergenceWarning,\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.7937619350732018"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count_vect = CountVectorizer()  # 单词出现次数\n",
    "X_train_counts = count_vect.fit_transform(twenty_train.data) # 训练集文本转换为词频矩阵\n",
    "X_test_count = count_vect.transform(twenty_test.data) # 测试集文本转换为词频矩阵\n",
    "model = LinearSVC() # 创建线性支持向量机分类模型\n",
    "model.fit(X_train_counts, twenty_train.target) # 训练模型\n",
    "predicted = model.predict(X_test_count) # 预测测试集标签\n",
    "np.mean(predicted == twenty_test.target) # 计算准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8701464035646085"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tf_vec = TfidfVectorizer()  # tf-idf\n",
    "X_train_tfidf = tf_vec.fit_transform(twenty_train.data) # 训练集文本转换为tf-idf矩阵\n",
    "X_test_tfidf = tf_vec.transform(twenty_test.data) # 测试集文本转换为tf-idf矩阵\n",
    "model = LinearSVC() # 创建线性支持向量机分类模型 \n",
    "model.fit(X_train_tfidf, twenty_train.target) # 训练模型\n",
    "predicted = model.predict(X_test_tfidf) # 预测测试集标签\n",
    "np.mean(predicted == twenty_test.target) # 计算准确率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: 'mlzukan-img.png'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_24960\\167518197.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mPIL\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mImage\u001b[0m \u001b[1;31m# 导入PIL库中的Image模块\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m \u001b[1;31m# 导入numpy库\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mimg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mImage\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'mlzukan-img.png'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconvert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'L'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 打开图像并转换为灰度模式\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[0mwidth\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheight\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msize\u001b[0m \u001b[1;31m# 获取图像的宽度和高度\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mimg_pixels\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m# 初始化一个空列表用于存储像素值\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\Users\\admin\\anaconda3\\envs\\ml\\lib\\site-packages\\PIL\\Image.py\u001b[0m in \u001b[0;36mopen\u001b[1;34m(fp, mode, formats)\u001b[0m\n\u001b[0;32m   3234\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3235\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mfilename\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 3236\u001b[1;33m         \u001b[0mfp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbuiltins\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"rb\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   3237\u001b[0m         \u001b[0mexclusive_fp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   3238\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'mlzukan-img.png'"
     ]
    }
   ],
   "source": [
    "# 图像数据转换处理\n",
    "from PIL import Image # 导入PIL库中的Image模块\n",
    "import numpy as np # 导入numpy库\n",
    "img = Image.open('xxx.png').convert('L') # 打开图像并转换为灰度模式，读者自行照一张图片放进去。\n",
    "width, height = img.size # 获取图像的宽度和高度\n",
    "img_pixels = [] # 初始化一个空列表用于存储像素值\n",
    "for y in range(height): # 遍历图像的每一行\n",
    "    for x in range(width):# 通过getpixel获取指定位置的像素值\n",
    "        img_pixels.append(img.getpixel((x,y))) # 将像素值添加到列表中\n",
    "print(img_pixels) # 输出像素值列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets # 导入数据集\n",
    "from sklearn import metrics # 导入评估指标\n",
    "from sklearn.ensemble import RandomForestClassifier # 导入随机森林分类模型\n",
    "digits = datasets.load_digits() # 加载手写数字数据集\n",
    "n_samples = len(digits.images) # 获取样本数量\n",
    "data = digits.images.reshape((n_samples, -1)) # 将图像数据转换为二维数组\n",
    "model = RandomForestClassifier() # 创建随机森林分类模型\n",
    "model.fit(data[:n_samples // 2], digits.target[:n_samples // 2]) # 训练模型，// 表示地板除\n",
    "expected = digits.target[n_samples // 2:] # 获取测试集的实际标签\n",
    "predicted = model.predict(data[n_samples // 2:]) # 预测测试集标签\n",
    "print(metrics.classification_report(expected, predicted)) # 输出分类报告"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
